The Ditto MongoDB Connector, built in partnership with MongoDB, provides seamless bidirectional synchronization between Ditto apps and MongoDB databases. It allows you to extend your data in MongoDB’s developer platform to the edge via Ditto’s powerful edge synchronization capabilities, effectively bridging the gap between edge and cloud data management.
_id
). To use permissions effectively, most Ditto applications use objects rather than strings as their _id
. These sub-fields with the _id
are then often duplicated in the document’s body to facilitate POJO/DTO-like patterns in your application code. For example you may choose to model an order (in a retail system) as follows:
ObjectIds
, essentially UUIDs with a time-based component; alternatively you may choose to use a single field value as the ID (e.g. orderId
). It’s very rare for IDs in MongoDB to be used in the same way as within Ditto, therefore IDs likely need to be handled differently in both systems.
The MongoDB Connector automatically bridges this difference in IDs, so that both systems can converge, even if their primary keys differ. It does this through the “ID Mapping” process, based on the configuration that you provide to the connector.
_id
field in the ID mapping.
In that case the ID fields used between Ditto and MongoDB will be identical.
However, some restrictions apply here:
NULL
cannot be present in the ID_id
field itself in this mode.
You must only refer to top-level fields in the document, nested fields are not supported.orderId
field and the following MongoDB document:
_id
field itself in this case.
_id
field itself in this mode.
You must only refer to top-level fields in the document, nested fields are not supported.location
and orderId
fields and the following MongoDB document:
date
objects, you can read more about the datatypes available in https://www.mongodb.com/docs/manual/reference/bson-types/.
Ditto represents its data using JSON with a few key additional datatypes (e.g. integer), which does not have the full set of datatypes available in BSON. The connector will perform automatic mapping of BSON datatypes to JSON when synchronizing data from MongoDB to Ditto.
The full set of mappings are listed below:
MongoDB BSON Datatype | Ditto JSON Datatype |
---|---|
Double | number |
String | string |
Object | object |
Array | array |
ObjectId | string |
Boolean | boolean |
Date | string (ISO8601 format) |
Null | null |
Regular Expression | object (see EJSON v2) |
32-bit Integer | integer |
64-bit Integer | integer |
Timestamp | number (epoch timestamp) |
Decimal128 | string |
changeStreamPreAndPostImages
set to true
. This setting allows the connector to ensure causal consistency between both systems, and the connector will not start if this is not set for all collections.
When creating new collections, pass changeStreamPreAndPostImages: true
to your creation command. For pre-existing collections, you can modify this configuration using collMod
.
See Enable Change Stream Pre and Post Images.
For example, to edit an existing collection called orders
, you can run the following command:
readWrite
permission for the specific database that you’re looking to synchronize with Ditto. See Configure MongoDB Database Users. If you are connecting to a sharded database (i.e. a mongos
connection) the database user also needs to have the explicit read
permission for the built-in collection
collection in the config
database, so it can inspect sharding configurations.
Settings
> MongoDB Connector
.
Connect
button for your cluster, ensuring that you use the credentials of the database user that you created in the prerequisites.The connection string must be supplied in the format mongodb+srv://<username>:<password>@<cluster>.mongodb.net/
.
For example to connect to the cluster with the URL my_cluster.mongodb.net
, using the username my_user
with a password of my_password
, you would provide mongodb+srv://my_user:my_password@my_cluster.mongodb.net/
.
Non-SRV-based connection strings cannot be used with the MongoDB connector.Add Collection
button, entering the collection name, toggling initial sync, then selecting how you would like your IDs mapped.Enabling initial sync will trigger an initial sync of the collection from MongoDB to Ditto, this is rate limited and will be done in the background to avoid affecting the performance of the connector. Once the initial sync is complete, the connector will continue to synchronize the collection on an ongoing basis via MongoDB Change Streams.For very large collections, you may opt to not rely on the Connector to perform the initial sync, and instead perform the initial sync manually via the Ditto API so that you have finer-grained control over the process.If you select the Match IDs
option, then the ID fields used between Ditto and MongoDB will be identical.
Map fields to Ditto ID
option, then you will need to add all of the fields that you wish to map to the Ditto ID.
Add collection
button to add the collection to the connector configuration, repeat this step for each collection you wish to synchronize with Ditto.Save
button to create the connector.Pending
while the connector is being created, then once it is ready it will show as Running
.
In case of an error, the connector will show as Failed
, and you will be able to see the error message in the Ditto Portal, see also Troubleshooting Connectivity.Failed
, and you will be able to see a more detailed error message in the Ditto Portal.
These checks are as follows:
changeStreamPreAndPostImages
set to true
Error Message | Remediation |
---|---|
Connection string provided is invalid. | Delete the connector and try again, ensuring that the connection string is correct. |
Failed to connect to MongoDB | • Ensure that the MongoDB cluster has the correct IP addresses added to the allowlist (see Add Ditto IPs to MongoDB Allowlist) • Ensure that the MongoDB database user exists and has the correct permissions (see Create a MongoDB Database User) |
Failed to list database names | Ensure that the MongoDB database user has the correct permissions (see Create a MongoDB Database User) |
Failed to list collections | Ensure that the MongoDB database user has the correct permissions (see Create a MongoDB Database User) |
Database <name> not found | Ensure that the MongoDB database exists (see Create MongoDB Database) |
Collection <name> not found | Ensure that the MongoDB collection exists (see Create MongoDB Collections) |
Collection <name> must be configured with changeStreamPreAndPostImages enabled | Ensure that the MongoDB collection has changeStreamPreAndPostImages set to true (see Create MongoDB Collections) |
Collection <name> cannot be a capped collection | Ensure that the MongoDB collection is not a capped collection (see Create MongoDB Collections) |
Collection <name> cannot be created | Ensure that the MongoDB database user has the correct permissions (see Create a MongoDB Database User) |
Something went wrong | An unknown error occurred, please contact Ditto support for assistance. |
Edit
button on the MongoDB Connector page in the Ditto Portal.
This will open a form with the current configuration, allowing you to modify the configuration as required:
Save
button to update the connector with the new configuration.
__ditto_connector_sessions
internal collection within your MongoDB database. These are periodically cleaned up by the connector when the sessions are no longer needed.__ditto_unsynced_documents
internal collection within your MongoDB database. These documents serve as a record for you to know which documents have failed to synchronize, and can be used to manually resync the documents. This collection is not automatically cleaned up by the connector, you can choose to delete the documents in this collection if you no longer need to keep track of these documents._
within the Ditto document)